//
//  InviteFriendViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/26.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "InviteFriendViewController.h"

@interface InviteFriendViewController ()
{
    enum WXScene _scene;
}
@property(nonatomic,strong)ShareData *data;
@end

@implementation InviteFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.shareView.layer.cornerRadius=8;
    self.shareView.clipsToBounds=YES;
    self.headImage.layer.cornerRadius=40;
    self.headImage.clipsToBounds=YES;
    [self getShareData];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
}

-(void)getShareData
{
    //http://192.168.1.107:3722/Common/PageInfo/{UserCode}/{PageType}/{ObjectID}/{Timestamp}/{Sign}
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,
                        @"PageType":@"INVITE_URL_GENSUC",
                        @"ObjectID":_categoryId,
                        @"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSString *urlString=[NSString stringWithFormat:@"Common/PageInfo/%@/%@/%@/%@/%@",GETUSERCODE,@"INVITE_URL_GENSUC",_categoryId,timeStamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"无法获取分享链接，请稍后再试"];
        }
        if ([responses[@"IsSuccess"]integerValue]==1) {
            NSDictionary *dic=responses[@"ReturnObject"];
            if (dic!=[NSNull null]) {
                _data=[[ShareData alloc]initWithDic:dic];
                self.descriptionLbl.text=dic[@"Description"];
                [self.headImage sd_setImageWithURL:[NSURL URLWithString:dic[@"HeadImg"]] placeholderImage:nil options:SDWebImageRetryFailed];
            }
            
        }else
        {
            [MBProgressHUD showError:dic[@"ReturnString"]];

        }

    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)aboutShare:(id)sender {
    WebViewController *web=[[WebViewController alloc]init];
    web.title=@"净净邀请返利";
    web.hidesBottomBarWhenPushed=YES;
    web.pageUrl=ABOUT_FANLI;
    [self.navigationController pushViewController:web animated:YES];
}

- (IBAction)shareWeiXinFriend:(id)sender {
    _scene=WXSceneSession;
    [self share];
}

- (IBAction)shareTimeLine:(id)sender {
    _scene=WXSceneTimeline;
    [self share];
}
#pragma mark--分享到微信
-(void)share
{
    WXWebpageObject *wxwebobj=[[WXWebpageObject alloc]init];
    wxwebobj.webpageUrl= _data.ShareUrl;
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = _data.ShareTitle;
    message.description=_data.ShareDes;
    NSURL *_url = [NSURL URLWithString: _data.ShareImg];// 获取的图片地址
    [message setThumbImage:[UIImage imageWithData: [NSData dataWithContentsOfURL:_url]]];
    message.mediaObject=wxwebobj;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.message = message;
    req.scene = _scene;
    [WXApi sendReq:req];
    
}

@end
